<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Calculate difference between given time and this objectʼs time</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="intlcalendar.equals.html">« IntlCalendar::equals</a></li>
      <li style="float: right;"><a href="intlcalendar.fromdatetime.html">IntlCalendar::fromDateTime »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.intlcalendar.html">IntlCalendar</a></li>
    <li>Calculate difference between given time and this objectʼs time</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="intlcalendar.fielddifference" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">IntlCalendar::fieldDifference</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8, PECL &gt;= 3.0.0a1)</p><p class="refpurpose"><span class="refname">IntlCalendar::fieldDifference</span> &mdash; <span class="dc-title">Calculate difference between given time and this objectʼs time</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-intlcalendar.fielddifference-description">
  <h3 class="title">说明</h3>
  <p class="para">
   面向对象风格
  </p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>IntlCalendar::fieldDifference</strong></span>(<span class="methodparam"><span class="type">float</span> <code class="parameter">$timestamp</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$field</code></span>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   过程化风格
  </p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>intlcal_field_difference</strong></span>(<span class="methodparam"><span class="type"><a href="class.intlcalendar.html" class="type IntlCalendar">IntlCalendar</a></span> <code class="parameter">$calendar</code></span>, <span class="methodparam"><span class="type">float</span> <code class="parameter">$timestamp</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$field</code></span>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Return the difference between the given time and the time this object is
   set to, with respect to the quantity specified the
   <code class="parameter">field</code> parameter.
  </p>
  <p class="para">
   This method is meant to be called successively, first with the most
   significant field of interest down to the least significant field. To this
   end, as a side effect, this calendarʼs value for the field specified is
   advanced by the amount returned.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-intlcalendar.fielddifference-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">calendar</code></dt>

    <dd>

     <p class="para"><span class="classname"><a href="class.intlcalendar.html" class="classname">IntlCalendar</a></span> 实例。</p>
    </dd>

   
   
    <dt>
<code class="parameter">timestamp</code></dt>

    <dd>

     <p class="para">
      The time against which to compare the quantity represented by the
      <code class="parameter">field</code>. For the result to be positive, the time
      given for this parameter must be ahead of the time of the object the
      method is being invoked on.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">field</code></dt>

    <dd>

     <p class="para">
      The field that represents the quantity being compared.
     </p>
     

<p class="para">
 One of the <span class="classname"><a href="class.intlcalendar.html" class="classname">IntlCalendar</a></span> date/time <a href="class.intlcalendar.html#intlcalendar.constants" class="link">field constants</a>. These are integer
 values between <code class="literal">0</code> and
 <strong><code>IntlCalendar::FIELD_COUNT</code></strong>.
</p>

    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-intlcalendar.fielddifference-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a (signed) difference of time in the unit associated with the
   specified field 或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>



 <div class="refsect1 examples" id="refsect1-intlcalendar.fielddifference-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2391">
   <p><strong>示例 #1 <span class="function"><strong>IntlCalendar::fieldDifference()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'date.timezone'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Europe/Lisbon'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'intl.default_locale'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'fr_FR'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$cal1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">fromDateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2012-02-29&nbsp;09:00:11'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cal2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">fromDateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2013-03-01&nbsp;09:19:29'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$cal2</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTime</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Time&nbsp;before:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"The&nbsp;difference&nbsp;in&nbsp;time&nbsp;is&nbsp;%d&nbsp;year(s),&nbsp;%d&nbsp;month(s),&nbsp;"<br />&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"%d&nbsp;day(s),&nbsp;%d&nbsp;hour(s)&nbsp;and&nbsp;%d&nbsp;minute(s)\n"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDifference</span><span style="color: #007700">(</span><span style="color: #0000BB">$time</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">FIELD_YEAR</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDifference</span><span style="color: #007700">(</span><span style="color: #0000BB">$time</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">FIELD_MONTH</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDifference</span><span style="color: #007700">(</span><span style="color: #0000BB">$time</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">FIELD_DAY_OF_MONTH</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDifference</span><span style="color: #007700">(</span><span style="color: #0000BB">$time</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">FIELD_HOUR_OF_DAY</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDifference</span><span style="color: #007700">(</span><span style="color: #0000BB">$time</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlCalendar</span><span style="color: #007700">::</span><span style="color: #0000BB">FIELD_MINUTE</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #FF8000">//now&nbsp;it&nbsp;was&nbsp;advanced&nbsp;to&nbsp;the&nbsp;target&nbsp;time,&nbsp;exception&nbsp;for&nbsp;the&nbsp;seconds,<br />//for&nbsp;which&nbsp;we&nbsp;did&nbsp;not&nbsp;measure&nbsp;the&nbsp;difference<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Time&nbsp;after:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IntlDateFormatter</span><span style="color: #007700">::</span><span style="color: #0000BB">formatObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$cal1</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Time before: 29 févr. 2012 09:00:11
The difference in time is 1 year(s), 0 month(s), 1 day(s), 0 hour(s) and 19 minute(s)
Time after: 1 mars 2013 09:19:11
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>